package com.yuke.cloud.service.pmc.controller.frontend;

import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.pmc.dto.ConditionDto;
import com.yuke.cloud.service.pmc.dto.ProdIdsDto;
import com.yuke.cloud.service.pmc.dto.ProdAttributeDto;
import com.yuke.cloud.service.pmc.entity.ProdAttribute;
import com.yuke.cloud.service.pmc.service.ProdAttributeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.service.pmc.service.ProdCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by zys on 2018/09/30.
*/
@Slf4j
@Api(value = "WEB - ProdAttributeController", tags = "处理ProdAttribute相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/prod/attribute")
public class ProdAttributeController extends BaseController {
    @Resource
    private ProdAttributeService prodAttributeService;
    @Resource
    private ProdCategoryService prodCategoryService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "prodAttribute", value = "该数据记录的POJO") @RequestBody ProdAttribute prodAttribute) {
        prodAttributeService.save(prodAttribute);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
//        Map<String, Integer> map = prodAttributeService.checkAttributeId(id);
//        if (map.get("prodSpecAttribute") > 0 || map.get("prodCategoryAttribute") > 0){
//            log.info("与其他表关联，不能删除");
//            Map<String, Integer> mapMessage = new HashMap<>();
//            mapMessage.put("商品规格属性表关联数", map.get("prodSpecAttribute"));
//            mapMessage.put("分类与属性关联表关联数", map.get("prodCategoryAttribute"));
//            return WrapMapper.error(mapMessage.toString());
//        }
        try {
            prodAttributeService.deleteById(id);
        } catch (DataIntegrityViolationException e) {
            return WrapMapper.error("数据正在使用，无法删除");
        }
        return WrapMapper.ok();
    }

    @PostMapping("/ids")
    @ApiOperation(httpMethod = "POST", value = "根据id删除多条记录")
    public Wrapper deleteByIds(@ApiParam(name = "attributeIds", value = "记录id") @RequestBody ProdIdsDto prodIdsDto) {
//        String[] ids = prodIdsDto.getIds().split(",");
//        for (String i:ids) {
//            Long attributeId = Long.valueOf(i);
//            Map<String, Integer> map = prodAttributeService.checkAttributeId(attributeId);
//            if (map.get("prodSpecAttribute") > 0 || map.get("prodCategoryAttribute") > 0){
//                return WrapMapper.error("与其他表关联，请先删除关联数据");
//            }
//        }
        try {
            prodAttributeService.deleteByIds(prodIdsDto.getIds());
        } catch (DataIntegrityViolationException e) {
            return WrapMapper.error("数据正在使用，无法删除");
        }
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "prodAttribute", value = "需要修改的内容") @RequestBody ProdAttribute prodAttribute) {
        prodAttributeService.update(prodAttribute);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<ProdAttribute> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        ProdAttribute prodAttribute = prodAttributeService.findById(id);
        List<Long> categoryIds = new ArrayList<>();
        categoryIds.add(id);
        prodCategoryService.parentCategoryId(id, categoryIds);
        return WrapMapper.ok(prodAttribute);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<ProdAttribute>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ProdAttribute> list = prodAttributeService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @PostMapping("/condition")
    @ApiOperation(httpMethod = "POST", value = "分页获取所有记录")
    public Wrapper<PageInfo<ProdAttributeDto>> getListsByCondition(@ApiParam(name = "categoryId", value = "记录id") @RequestBody ConditionDto conditionDto,
                                                                   @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                   @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ProdAttributeDto> list = prodAttributeService.getListsByCondition(conditionDto.getCategoryId(), conditionDto.getAttributeName());
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }
    @PostMapping("/ifExist")
    @ApiOperation(httpMethod = "POST", value = "判断属性名是否已存在")
    public Wrapper ifExist(@ApiParam(name = "id", value = "记录id") @RequestBody ProdAttribute prodAttribute) {
        int count = prodAttributeService.selectCount(prodAttribute);
        return WrapMapper.ok(count);
    }
}
